<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class BaseController extends Controller
{
    public function dluoRandomString($length = 60) {
        return Str::random($length >= 1 ? $length : 60);
    }

    public function dluoDateFull($time = null) {
        return date('Y-m-d H:i:s', is_null($time) ? time() : $time);
    }

    public function dluoObject($code = 0, $message = 'OK', $data = null) {
        $retData = [];
        $retData[config('api.json.errcode_key', 'errcode')] = $code;
        $retData[config('api.json.message_key', 'message_key')] = $message;
        
        if (isset($data)) {
            $retData['data'] = $data;
        }
        
        return $retData;
    }

    private function dluoJson($data) {
        return response()->json($data);
    }

    public function dluoResponse($code, $message, $data = null) {
        return self::dluoJson(self::dluoObject($code, $message, $data));
    }

    public function dluoSuccess($data = null) {
        return self::dluoJson(self::dluoObject(null, null, $data));
    }

    public function dluoSuccessMessage($message) {
        return self::dluoJson(self::dluoObject(null, $message, $data));
    }

    public function dluEorror($code, $message = 'ERROR') {
        return rself::dluoJson(self::dluoObject($code, $message));
    }

    public function dluoException($exception, $exceptionMessage) {
        return self::dluoJson(self::dluoObject(40000, $exceptionMessage . ' exception.', $exceptionMessage));
    }

    public function dluoLogError($method, $e) {
        Log::error(__METHOD__ . ' :: ' . $e->getMessage());
    }

    public function dluoLogException($method, $e) {
        $this->logError($method, $e);
        return $this->exception($method, $e);
    }
}